package cn.player.act;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import cn.player.R;
import cn.player.media.entity.VideoInfo;
import cn.player.media.fragment.MediaPlayVFragment;
import cntv.player.core.util.ToastUtil;

/**
 * 可横屏可竖屏
 *
 * @author zengliang
 */
public class MediaPlayStarActivity extends BaseActivity {

    private VideoInfo videoInfo;
    private MediaPlayVFragment mpFrag;
    private OnClickListener mOnClickListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.media_play_star_layout);

        Log.i("zl", "MediaPlayActivity onCreate");

        videoInfo = (VideoInfo) getIntent().getSerializableExtra(VideoInfo.class.getName());
        if (videoInfo == null) {
            playError(0, "MediaPlayActivity get VideoInfo null...");
            return;
        }

        setListener();

        addPlayer();

    }

    private void removePlayer() {
        if (mpFrag != null && mpFrag.isAdded()) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.remove(mpFrag);
            ft.commit();
        }
    }

    /**
     * 添加播放器
     */
    private void addPlayer() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (mpFrag == null) {
            mpFrag = MediaPlayVFragment.newInstance(videoInfo);
        }
        if (mpFrag.isAdded()) {
            ToastUtil.showToast(mContext, "您已经添加播放器了....", Toast.LENGTH_SHORT);
            return;
        }

        ft.add(R.id.fl_play_area, mpFrag); // 在id为ll_play_area的LinearLayout布局中显示mpFrag
        ft.commit();
        mpFrag.setOnPlayerListener(new MediaPlayVFragment.OnPlayerListener() {

            @Override
            public void onPlayStart() {

            }

            @Override
            public void onPlayPauseChanged(boolean isPaused) {

            }

            @Override
            public void onPlayError(int errorNo, String errMsg) {
                Log.i("zl", "mediaplayActivity onPlayError,errorNo = " + errorNo + ",errMsg = " + errMsg);
                removePlayer();
                playError(errorNo, errMsg);
            }

            @Override
            public void onPlayComplete() {

            }

        });
    }


    private void setListener() {
        mOnClickListener = new OnClickListener() {

            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.btn_click_to_play:
                        addPlayer();
                        break;

                    default:
                        break;
                }
            }
        };
    }


    /**
     * 弹框提示播放错误
     */
    private void playError(int errNo, String errMsg) {
        new AlertDialog.Builder(this).setTitle("提示").setMessage(errMsg)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
//				finish();
                    }
                }).show();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            this.finish();
            overridePendingTransition(R.anim.base_push_stay, R.anim.base_push_down_out);
            return false;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
